class Solution {
    public int numTrees(int n) {
        int[] db = new int[n + 1];
        db[0] = 1;
        db[1] = 1;
        for (int i = 2; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                db[i] += db[j - 1] * db[i - j];
            }
        }
        return db[n];
    }
}